package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.Activity;
import com.example.zqm.git72.JavaBean.Collection;
import com.example.zqm.git72.JavaBean.Train;
import com.example.zqm.git72.Model.TrainModel;
import com.example.zqm.git72.R;
import com.example.zqm.git72.Service.ActivityService;
import com.example.zqm.git72.Service.TrainService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.zqm.git72.R.id.district_name;
import static com.example.zqm.git72.R.id.in;
import static com.example.zqm.git72.R.id.train;

public class TrainDetailActivity extends AppCompatActivity {
    String id;
    private TextView intro;
    private TextView traintime;
    private TextView place;
    private TextView price;
    private ImageView trainpic;
    private TextView notice;
    private TextView name;
    private TextView tel;
    private ImageButton collect;
    Retrofit retrofit;
    String train_id;
    String user_id;
    String ctrainid;
    int fag;
    Collection collection;
    public Train train;
   private TextView enroll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traindetail);
        intNN();
        initview();
        iscollect();
    }
    public  void  initview(){
        intro = (TextView)findViewById(R.id.intro);
        traintime  = (TextView)findViewById(R.id.traintime);
        place =(TextView)findViewById(R.id.place);
        price=(TextView)findViewById(R.id.price);
        notice=(TextView)findViewById(R.id.notice);
        name=(TextView)findViewById(R.id.name);
        trainpic=(ImageView)findViewById(R.id.trainypic);
        tel =(TextView)findViewById(R.id.tel);
        collect=(ImageButton)findViewById(R.id.collect);
        enroll=(TextView)findViewById(R.id.enroll);
        enroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(TrainDetailActivity.this,TrainOrderActivity.class);
                String activityname = name.getText().toString();
                String activityprice = price.getText().toString();
                String activityintro = intro.getText().toString();
                intent.putExtra("trainname",activityname);
                intent.putExtra("trainprice",activityprice);
                intent.putExtra("trainintro",activityintro);
                intent.putExtra("id",train_id);
                startActivity(intent);
            }
        });
    }

    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        train_id=bundle.getString("id");
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id=sharedPreferences.getString("user_id","");
        TrainModel trainModel= new TrainModel();
        Call<Train> call = trainModel.getTrainById(train_id);
        call.enqueue(new Callback<Train>() {
            @Override
            public void onResponse(Call<Train> call, Response<Train> response) {
                train=response.body();
                intro.setText(train.getIntro());
                notice.setText(train.getNotice());
                traintime.setText(train.getTraintime());
                place.setText(train. getPlace());
                price.setText("￥"+train. getPrice());
                name.setText(train.getName());
                tel.setText(train.getTel());
                String picUrl = "http://172.24.10.175/workout/Uploads/" + train.getThumb();
                Picasso.with(TrainDetailActivity.this).load(picUrl.toString())
                        .resize(200,50).into(trainpic);
            }
            @Override
            public void onFailure(Call<Train> call, Throwable t) {

            }
        });
    }


     protected  void iscollect(){
         collect.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 TrainModel trainModel =new TrainModel();
                 Call<Integer> call = trainModel.iscollect(train_id,user_id);
                 call.enqueue(new Callback<Integer>() {
                     @Override
                     public void onResponse(Call<Integer> call, Response<Integer> response) {
                         if(response.isSuccessful()){

                             Toast.makeText(TrainDetailActivity.this,"关注成功",Toast.LENGTH_SHORT).show();
                             Picasso.with(TrainDetailActivity.this).load(R.drawable.shoucang).into(collect);
                         }

                     }

                     @Override
                     public void onFailure(Call<Integer> call, Throwable t) {

                     }
                 });
             }
         });

     }



}
